//
//  SettingsViewController.m
//  家校互通
//
//  Created by 李赛 on 15/6/15.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "SettingsViewController.h"
#import "SettingTableViewCell.h"
#import <SDWebImageManager.h>
#import "ViewController.h"
@interface SettingsViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property (nonatomic, retain)NSMutableArray  *titleArray;

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"设置";
    _contenttableView.delegate=self;
    _contenttableView.dataSource=self;
    
    _titleArray = [NSMutableArray arrayWithObjects:@"给好评",@"常见问题",@"清除缓存",@"声明",@"注销",nil];

    // Do any additional setup after loading the view.
}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        SettingTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"SettingTableViewCell" forIndexPath:indexPath];
       vCell.titleLB.text=[_titleArray objectAtIndex:indexPath.section];
        UIView *selectView=[[UIView alloc]initWithFrame:vCell.frame];
        selectView.backgroundColor=COLOR(240, 240, 240);
        vCell.selectedBackgroundView=selectView;
        return vCell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        return _titleArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    SettingTableViewCell *vCell=(SettingTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if ([vCell.titleLB.text isEqualToString:@"清除缓存"]) {
        
        CGFloat clearDiskSize=[[SDImageCache sharedImageCache]getSize]/(1024*1024);
     
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            NSString *clearSize=[NSString stringWithFormat:@"为您清除%@M缓存",@(clearDiskSize)];
            UIAlertView *alerView=[[UIAlertView alloc]initWithTitle:@"清除缓存" message:clearSize delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alerView show];
        
        }];
    }else if([vCell.titleLB.text isEqualToString:@"注销"])
    {
        [DSFUserInfo userLogout:^(BOOL result, NSError *) {
            if (result) {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:@"" forKey:@"username"];
                [defaults setObject:@"" forKey:@"password"];
                [defaults setObject:@(0) forKey:@"userType"];
                [defaults synchronize];
                UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                ViewController *vc = [storyboard  instantiateViewControllerWithIdentifier:@"ViewController"];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
